package com.example.neusoft.sqlitedemo;

import android.content.Context;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;

public class AddContactActivity extends AppCompatActivity {

    EditText edit_name, edit_tel, edit_type;
    Button button_save;
    Context context;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_add_contact);
        context = AddContactActivity.this;
        initView();

        button_save.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                // TODO Auto-generated method stub
                String name = edit_name.getText().toString();
                String tel = edit_tel.getText().toString();
                String type = edit_type.getText().toString();

                if (TextUtils.isEmpty(name)) {
                    edit_name.setError("请输入名字");
                    return;
                }
                if (TextUtils.isEmpty(tel)) {
                    edit_tel.setError("请输入电话");
                    return;
                }
                if (TextUtils.isEmpty(type)) {
                    edit_type.setError("请输入类型");
                    return;
                }

                ContactBean contactBean = new ContactBean();
                contactBean.setName(name);
                contactBean.setTel(tel);
                contactBean.setType(type);

                ContactDBAdapter dbAdapter = ContactDBAdapter.getInstance(context);
                dbAdapter.insert(contactBean);

                finish();// 关闭当前的activity

            }
        });


    }

    void initView() {
        edit_name = (EditText) findViewById(R.id.edittext_addcontact_name);
        edit_tel = (EditText) findViewById(R.id.edittext_addcontact_tel);
        edit_type = (EditText) findViewById(R.id.edittext_addcontact_type);
        button_save = (Button) findViewById(R.id.btn_addcontact_save);
    }

}
